<script>


export default {
  name: "MenuList",
  data() {
    return {
      queryInfo: {
        keyword: '',
        currentPage: 1,
        pageSize: 8
      },
      total: 0,
      roles: [],
      listLoading: false,
      sels: [],
      editFormVisible: false,

      editForm: {
        id: '',
        name: '',
        url: '',
        icon: '',
        parent: {
          id: '',
          name: '',
          url: '',
          icon: ''
        }
      },
      editLoading: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      menuTree: [],
      menuList: [],
    }
  },
  methods: {
    getMenu() {
      this.$http.post("/menu", this.queryInfo).then(res => {
        if (res.data.success) {
          this.roles = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.$message({
            message: res.data.msg,
            type: "error",
            showClose: true
          })
        }
      }).catch(err => {
        this.$message({
          message: "请求异常",
          type: "error",
          showClose: true
        })
      }).finally(() => {
        this.listLoading = false;
      })
    },
    handleAdd() {
      this.editFormVisible = true;
      // this.$refs["editForm"].resetFields();
      this.editForm = {
            id: '',
            name: '',
            url: '',
            icon: '',
            parent: {
              id: '',
              name: '',
              url: '',
              icon: ''
        }
      };
      this.getParentMenus();
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      this.getMenu();
    },
    selsChange(sels) {
      this.sels = sels
    },
    batchRemove() {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let ids = this.sels.map(item => item.id);
        // console.log(ids)
        this.$http.patch("/menu", ids).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getMenu();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    handleCurrentChange(val) {
      this.queryInfo.currentPage = val;
      this.getMenu();
    },
    handleEdit(index, row) {
      console.log(row);
      this.editFormVisible = true;
      this.getParentMenus(row.id);
      this.editForm = Object.assign({}, row);
      if (!this.editForm.parent) {
        this.editForm.parent = {
          id: null,
          name: '',
          url: '',
        }
      }
      console.log(this.editForm)
    },
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/menu/" + row.id).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getMenu();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    editSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.editLoading = true;
          this.$http.put("/menu", this.editForm).then(res => {
            if (res.data.success) {
              this.editFormVisible = false;
              this.$message({
                message: res.data.msg,
                type: "success",
                showClose: true
              })
              this.getMenu();
            }
          }).catch(err => {
            this.$message({
              message: "请求异常",
              type: "error",
              showClose: true
            })
          }).finally(() => {
            this.editLoading = false;
          })
        }
      })
    },
    getParentMenus(id) {
      this.$http.get("/menu/list", {params: {id: id}}).then(res => {
        if (res.data.success) {
          this.menuList = res.data.data;
        }
      }).catch(error => {
        this.$message({
          message: "父级菜单获取失败",
          type: "error",
          showClose: true
        })
      })
    }

  },
  mounted() {
    this.getMenu()
  }
}
</script>

<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="queryInfo.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getMenu">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="roles" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              max-height="460px"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" width="200" sortable/>
      <el-table-column prop="url" label="路径" width="200" sortable/>
      <el-table-column prop="icon" label="图标" width="140" sortable/>
      <el-table-column prop="parent.name" label="父菜单" width="140" sortable/>
      <el-table-column prop="parent.url" label="父菜单路径" width="140" sortable/>
      <el-table-column label="操作" width="240">
        <template v-slot="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.currentPage"
          :page-sizes="[3, 5, 8, 10, 20]"
          :page-size="8"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editFormVisible">
<!--      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">-->
      <el-form :model="editForm" label-width="80px" ref="editForm">
        <el-form-item label="菜单名称">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="intro">
          <el-input v-model="editForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="editForm.icon" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="父级菜单" prop="icon">
          <el-select v-model="editForm.parent.id" clearable placeholder="请选择">
            <el-option
                v-for="item in menuList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.url }}</span>
            </el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button></div>
    </el-dialog>
  </section>
</template>

<style scoped>
.dialogDiv {
  height: 200px;
  overflow: auto;
}
</style>
